﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Module;
using BLL.Common;

/// <summary>
///AppAuthHelper 的摘要说明
/// </summary>
public class AppAuthHelper
{

    public static List<AreaInfo> allArea = new List<AreaInfo>();
	public AppAuthHelper()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    /// <summary>
    /// 获取当前用户IP地址
    /// </summary>
    public static string CurrentUserIP
    {
        get
        {
            return HttpContext.Current.Request.UserHostAddress;
        }
    }

    /// <summary>
    /// 获取当前用户的远程DNS名称
    /// </summary>
    public static string CurrentUserHostName
    {
        get
        {
            return HttpContext.Current.Request.UserHostName;
        }
    }

    /// <summary>
    /// 获取当前用户的UA信息
    /// </summary>
    public static string CurrentUserAgent
    {
        get
        {
            return HttpContext.Current.Request.UserAgent;
        }
    }

    /// <summary>
    /// 获取当前登录用户信息
    /// </summary>
    public static UserPassportInfo CurrentUserInfo
    {
        get
        {
            if (HttpContext.Current.Session["LoginUserInfo"] != null)
            {
                return (UserPassportInfo)HttpContext.Current.Session["LoginUserInfo"];
            }
            else
            {
                return null;
            }
        }
    }

    /// <summary>
    /// 获取当前登录用户ID
    /// </summary>
    public static long CurrentUserPassportId
    {
        get
        {
            if (HttpContext.Current.Session["LoginUserInfo"] != null)
            {
                return ((UserPassportInfo)HttpContext.Current.Session["LoginUserInfo"]).UserPassportId;
            }
            else
            {
                return 0;
            }
        }
    }

    /// <summary>
    /// 当前用户Session检验
    /// </summary>
    /// <param name="page">当前页面回话</param>
    /// <param name="moduleId">当前操作模块ID</param>
    public static bool SessionCheck()
    {
        if (HttpContext.Current.Session["LoginUserInfo"] == null)
        {        
            return false;
        }
        return true;
    }

    public static void GoLogin()
    {
        string path = Contexts.AppPath + "/LoginApp.aspx";
        string thisUrl = Contexts.ThisPageUrlWithParams;
        path += "?"+Contexts.GoToUrl+"=" + HttpContext.Current.Server.UrlEncode(thisUrl);
        HttpContext.Current.Response.Redirect(path, true);
    }

    public static string GetLoginUrl()
    {
        string path = Contexts.AppPath + "/LoginApp.aspx";
        string thisUrl = Contexts.ThisPageUrlWithParams;
        path += "?" + Contexts.GoToUrl + "=" + HttpContext.Current.Server.UrlEncode(thisUrl);
        return path;
    }

    /// <summary>
    /// 获取所有行政区域
    /// </summary>
    /// <returns></returns>
    public static List<AreaInfo> GetAllArea()
    {
        if (allArea == null || allArea.Count == 0)
        {
            allArea = BLL.BArea.getInstance().GetALLArea();
        }
        return allArea;
    }
}